package com.svo.weitu.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.svo.platform.picbrowser.PicBrowseActivity;
import com.svo.weitu.activity.PicBrowser;
import com.svo.weitu.model.entity.ListEntity;

import java.util.ArrayList;

public class MyUtil {
	public static ArrayList<String> arrToList(String[] arr) {
		ArrayList<String> list = new ArrayList<>();
		if (arr == null) {
			return list;
		}
		for (String string : arr) {
			list.add(string);
		}
		return list;
	}
	public static String getDevId(Context context) {
		String devId =  ((TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		if (TextUtils.isEmpty(devId) || devId.length() < 10) {
			return getMac(context);
		}else {
			return devId;
		}
	}

	public static String getMac(Context context) {
		//wifi mac地址
		try {
			WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			String wifiMac = info.getMacAddress().replace(":", "");
			return "mac"+wifiMac;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "123456789123456";
	}
	/**
	 * 网络是否可用
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (mConnectivityManager == null) {
				return false;
			}else {
				NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
				if (mNetworkInfo != null) {
					return mNetworkInfo.isAvailable();
				}
			}
		}
		return false;
	}

	/**
	 * @param entity
	 * @param pics
	 */
	public void handlePics(final ListEntity entity, ArrayList<String> pics,Activity activity,ArrayList<ListEntity> infos) {
		Intent intent = new Intent(activity, PicBrowser.class);
		intent.putExtra(PicBrowseActivity.PICS, pics);
		intent.putExtra("id", entity.getId());
		intent.putExtra("title", entity.getTitle());
		intent.putExtra("entity", entity);
		activity.startActivity(intent);
	}
}
